package com.appcenter.application.mapper;

import com.appcenter.application.input.OrgUser.GetOrgUsersInput;
import com.appcenter.application.model.Orgs;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

public class OrgUsersProvider {
    public String searchOrgUsers(GetOrgUsersInput input){

        StringBuffer sql = new StringBuffer("select a.* from orgusers a inner join users b on a.UserId=b.Id where 1=1 ");
        if(input.getAppCode()==null){
            sql.append(" AND a.AppCode is null");
        }else{
            sql.append(" AND a.AppCode = #{AppCode}");
        }

        if(input.getUserId()!=null){
            sql.append(" AND b.Id=#{UserId}");
        }

        if(input.getKeyword()!=null){
            sql.append(" AND (b.UserCode like concat('%',#{keyword},'%') or b.UserName like concat('%',#{keyword},'%'))");
        }

        if(input.getOrgId()!=null){
            if(input.getPenetrate()){
                sql.append(" and (a.OrgId=#{OrgId} or a.OrgCodePath like concat('%',#{OrgCodePath},'%'))");

            }else{
                sql.append(" and a.OrgId=#{OrgId}");
            }
        }
        return sql.toString();
    }


}
